/**
 * author:Administrator
 * date:2016/3/17
 * desc:TODO
 */


"use strict";
var sequelize = require('../../persist/mysql/conn/sequelize.js');
var Sequelize = require('sequelize');

let DEFAULT_PAGEINDEX = 1;
let DEFAULT_PAGESIZE = 10;

let rawQuery = function * (sql, opt) {
  let result = yield sequelize.query(sql, opt);
  if (Array.isArray(result) && result.length > 0) {
    return result[0];
  }
  return result;
};

module.exports = rawQuery;

class RawQuery {

  * query(sql, opt) {
    console.log('query,sql=%s', sql);
    let result = yield sequelize.query(sql, opt);
    if (Array.isArray(result) && result.length > 0) {
      return result[0];
    }
    return result;
  }

  // 计算分页
  offset(pageIndex, pageSize) {
    pageIndex = pageIndex || DEFAULT_PAGEINDEX;
    pageSize = pageSize || DEFAULT_PAGESIZE;
    let start = (pageIndex - 1) * pageSize;
    let end = start + pageSize;
    return {start: start, end: end};
  }
}

module.exports = RawQuery;